﻿package homework01;

import javax.imageio.IIOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

    public  static void  sop(Object o){
        System.out.println(o);
    }

    public static void write(FileOutputStream fos,String s){
        byte arr[] = s.getBytes();
        try {
            fos.write(arr);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            }catch (IOException e1){
                e1.printStackTrace();
            }
        }
    }

    public static void read(FileInputStream fis){
        int len = 0;
        try {
            byte b[] = new byte[fis.available()];
            while ((len = fis.read(b)) != -1) {
                sop(new String(b)); 
            }
        } catch (IOException e) {

            e.printStackTrace();

        }
        finally{
            try {
                fis.close();
            }catch (IOException e1){
                e1.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("test.txt"),true);
            fis = new FileInputStream("test.txt");
        }catch (IOException e){
            e.printStackTrace();
        }
        write(fos,"Hello World");
        read(fis);
    }
}
